package com.ssy.lingxi.contract.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.ssy.lingxi.component.rabbitMQ.constants.MessageConstants;
import com.ssy.lingxi.contract.api.model.constant.ContractConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManager;
import java.util.HashMap;
import java.util.Map;

/**
 * 合同服务配置类
 */
@Configuration
public class ContractServiceConfig {

    @Bean
    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);}

    // ******************************************************采购竞价**********************************************************************
    //延时队列Exchange类型：CustomExchange
    @Bean
    Queue contract_order_change_queue() {
        return new Queue(ContractConstants.CONTRACT_ORDER_CHANGE_QUEUE, true, false, false);
    }

    @Bean
    DirectExchange contract_order_change_exchange() {
        return new DirectExchange(ContractConstants.CONTRACT_ORDER_CHANGE_EXCHANGE, true, false);
    }

    @Bean
    Binding contract_order_change_binding() {
        return BindingBuilder.bind(contract_order_change_queue()).to(contract_order_change_exchange()).with(ContractConstants.CONTRACT_ORDER_CHANGE_ROUTINGKEY);
    }


    // ******************************************************物料价格信息变更**********************************************************************
    //延时队列Exchange类型：CustomExchange
    @Bean
    Queue goodsPriceChangeQueue() {
        return new Queue(ContractConstants.GOODS_PRICE_CHANGE_QUEUE, true, false, false);
    }

    @Bean
    CustomExchange goodsPriceChangeExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //设置手动应答模式，消息持久化、死信消息
        return new CustomExchange(ContractConstants.GOODS_PRICE_CHANGE_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 交换机绑定
     */
    @Bean
    Binding goodsPriceBinding(Queue goodsPriceChangeQueue, CustomExchange goodsPriceChangeExchange) {
        return BindingBuilder.bind(goodsPriceChangeQueue).to(goodsPriceChangeExchange).with(ContractConstants.GOODS_PRICE_CHANGE_ROUTING_KEY).noargs();
    }


}
